package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.beancontext.BeanContext;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavor;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    public PageResult selectPage(DishPageQueryDTO dishPageQueryDTO) {
        //利用pageHelper进行操作
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.selectPage(dishPageQueryDTO);
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    @Override
    @Transactional
    public void addDish(DishDTO dishDTO) {
        //1、得到dishDTO的参数
        Dish dish = new Dish();
        //copy得到dish的部分属性
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.addDish(dish);
        //获取菜品id
        Long Id=dish.getId();
        //得到口味的集合
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        //将这些口味添加到表中
        //先判断集合是否为空
        if(dishFlavorList!=null&&dishFlavorList.size()>0){
            for (DishFlavor dishFlavor : dishFlavorList) {
                dishFlavor.setDishId(Id);
            }
        }
        dishFlavor.insertBatch(dishFlavorList);


    }

    @Override
    public void deleteDish(List<Long> dishIds) {
        //获取该菜品的信息
        for (Long dishId : dishIds) {
            Dish dish = dishMapper.findById(dishId);
            //判断该菜品是否存在起售的情况
            if(dish.getStatus()== StatusConstant.ENABLE){
                //抛出异常
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //获取套餐信息
        List<Long> setmealIds = setmealDishMapper.getSetmeal(dishIds);
        //如果存在套餐关联信息，删除失败
        if(setmealIds!=null&&setmealIds.size()>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //获取dishIds的一些信息
        for (Long dishId : dishIds)
        {
            //删除该菜品的所有信息
            dishMapper.deleteDish(dishId);
            dishFlavor.deleteDishFlavor(dishId);
        }
    }

    @Override
    public DishVO selectDishById(Long id) {
        //根据id查询菜品数据
        Dish dish = dishMapper.findById(id);

        //根据菜品id查询口味数据
        List<DishFlavor> dishFlavors = dishFlavor.getByDishId(id);//后绪步骤实现

        //将查询到的数据封装到VO
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        dishVO.setFlavors(dishFlavors);

        return dishVO;
    }

    @Override
    public void updateDish(DishDTO dishDTO) {
        //将dishDTO的属性值赋值给dish
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        Long Id=dish.getId();
        //删除该菜品的flavors
        dishFlavor.deleteDishFlavor(Id);
        //调用mapper进行修改
        dishMapper.updateDish(dish);
        //获取现在的flavors,完善其dish_Id属性
        List<DishFlavor> dishFlavorList = dishDTO.getFlavors();
        if(dishFlavorList!=null&&dishFlavorList.size()>0){
            for (DishFlavor dishFlavor : dishFlavorList) {
                dishFlavor.setDishId(Id);
            }
            //将口味添加进去
            dishFlavor.insertBatch(dishFlavorList);
        }
    }

    /**
     * 菜品起售停售
     *
     * @param status
     * @param id
     */
    @Transactional
    public void startOrstop(Integer status, Long id) {
        Dish dish = Dish.builder()
                .id(id)
                .status(status)
                .build();
        dishMapper.updateDish(dish);

        if (status == StatusConstant.DISABLE) {
            // 如果是停售操作，还需要将包含当前菜品的套餐也停售
            List<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            // select setmeal_id from setmeal_dish where dish_id in (?,?,?)
            List<Long> setmealIds = setmealDishMapper.getSetmeal(dishIds);
            if (setmealIds != null && setmealIds.size() > 0) {
                for (Long setmealId : setmealIds) {
                    Setmeal setmeal = Setmeal.builder()
                            .id(setmealId)
                            .status(StatusConstant.DISABLE)
                            .build();
                    setmealMapper.update(setmeal);
                }
            }
        }
    }

    @Override
    public List<Dish> list(Long categoryId) {
        Dish dish = Dish.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        return dishMapper.list(dish);
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    @Override
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavor.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
